const app = getApp()
const{
  GetSubscriptionInfo,
} =require('../../api/user')
const{
  SubscriptionInfo,
} =require('../../api/template')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showVip:true,
    userInfo:null,
    isLogin:false,
    unread:0,
    subscription_text:'订阅任务通知',
    tmplIds:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      userInfo:app.userInfo,
      isLogin:app.isLogin
    })
    this.init()
  },
  init(){
    if (this.data.isLogin) {
      GetSubscriptionInfo({tmpl_type:1}).then(res=>{

        this.setData({
          tmplIds: res.data.tmplIds,
          subscription_text:res.data.subscription_text,
          is_subscription:res.data.is_subscription
        })
        wx.hideLoading({
          complete: (res) => {},
        })
        wx.hideNavigationBarLoading(); //完成停止加载图标
        wx.stopPullDownRefresh();
        
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

    this.setData({
      userInfo:app.userInfo,
      isLogin:app.isLogin
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    this.setData({
      userInfo:app.userInfo,
      isLogin:app.isLogin
    })
    this.init()
  },
  subscriptionOrCancel(){
    // wx.requestSubscribeMessage({
    //   tmplIds: this.data.tmplIds,
    //   success (res) { 

    //   }
    // })
    wx.navigateTo({  
      url:'/pages/wchat/index' 
    });
    return
    wx.login({
      success (response) {
        if (response.code) {
          let code = response.code
          wx.getUserInfo({
            success: res => {
              SubscriptionInfo({code:code,iv:res.iv,encryptedData:res.encryptedData}).then(res=>{
              
                wx.hideLoading({
                  complete: (res) => {},
                })
              })
              
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
    

  },
  myPushTask() {
    if(!app.isLogin){
      wx.navigateTo({
        url: '/pages/login/index'
      })
      return
    }
    wx.navigateTo({
      url: `/pages/myPushTask/index`,
    })
  },
  wxLogin() {
    wx.navigateTo({
      url: '/pages/login/index'
    })
  },
  tabcontactUs() {
    wx.navigateTo({
      url: `/pages/doTask/index?pn=问题反馈&id=0&task_id=0&do_type=1`,
    })
  },
  tapEditProfilePage() {
    if(!app.isLogin){
      wx.navigateTo({
        url: '/pages/login/index'
      })
      return
    }
    wx.navigateTo({
      url: `/pages/myProfile/index`,
    })
  },
  tabAddDirector() {
    if(!app.isLogin){
      wx.navigateTo({
        url: '/pages/login/index'
      })
      return
    }
    wx.navigateTo({
      url: `/pages/myAdd/index`,
    })
  },
  tabweather(){
    wx.navigateTo({  
      url:'/pages/myweather/index' 
    });
  },
  tapMyTaskList(){
    if(!app.isLogin){
      wx.navigateTo({
        url: '/pages/login/index'
      })
      return
    }
    app.showTask = "2"
    wx.switchTab({  
      url:'/pages/index/index'  
    });
  },
  tapMyAttendance(){
    if(!app.isLogin){
      wx.navigateTo({
        url: '/pages/login/index'
      })
      return
    }
    wx.navigateTo({
      url: '/pages/myAttendance/index',
    })
  },
  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
/**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '绿京华',
    }
  }
})